package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/17 20:51
 */
@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {
   @Autowired
    DtsCategoryMapper categoryMapper;
   @Autowired
    DtsBrandMapper brandMapper;


    @Override
    public List<CatVo> findCategoryList() {

        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted,false);
        wrapper.eq(DtsCategory::getLevel,"L1");
//       获取所有的一级分类
        List<DtsCategory> categoryList = categoryMapper.selectList(wrapper);

        List<CatVo> catVoList = categoryList.stream().map(dtsCategory -> {
            CatVo catVo = new CatVo();
            catVo.setValue(dtsCategory.getId());
            catVo.setLabel(dtsCategory.getName());
//
//            根据一级分类id查询对应的二级分类
            LambdaQueryWrapper<DtsCategory> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DtsCategory::getDeleted,false);
            wrapper1.eq(DtsCategory::getLevel,"L2");
            wrapper1.eq(DtsCategory::getPid,dtsCategory.getId());

            List<DtsCategory> dtsCategoryList = categoryMapper.selectList(wrapper1);

            List<CatVo> collect = dtsCategoryList.stream().map(category -> {
                CatVo catVo1 = new CatVo();
                catVo1.setLabel(category.getName());
                catVo1.setValue(category.getId());
                return catVo1;
            }).collect(Collectors.toList());

            catVo.setChildren(collect);
            return catVo;
        }).collect(Collectors.toList());


        return catVoList;
    }

    @Override
    public DtsCategory findById(Integer categoryId) {
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted,false);
        wrapper.eq(DtsCategory::getId,categoryId);
        DtsCategory category = categoryMapper.selectOne(wrapper);
        return category;
    }

    @Override
    public Map<String, Object> selectByPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        IPage<DtsCategory> iPage = new Page<>(page,limit);
        QueryWrapper<DtsCategory> wrapper = new QueryWrapper<>();
        if ("desc".equalsIgnoreCase(order)) {
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }
        wrapper.eq("deleted",false);
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (id!=null){
            wrapper.like("id",id);
        }
        categoryMapper.selectPage(iPage,wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());
        return map;
    }

    @Override
    public List<Map<String, Object>> findCategoryL1List() {
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getLevel,"L1");
        wrapper.eq(DtsCategory::getDeleted,false);
        List<DtsCategory> list = categoryMapper.selectList(wrapper);
        List<Map<String, Object>> collect = list.stream().map(category -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", category.getId());
            map.put("label", category.getName());
            return map;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void update(DtsCategory category) {
        categoryMapper.updateById(category);
    }

    @Override
    public void insert(DtsCategory category) {
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getName,category.getName());
        Integer count = categoryMapper.selectCount(wrapper);

        if (count>0){
            throw new RuntimeException("类目名称已存在");
        }
        categoryMapper.insert(category);
    }

    @Override
    public void delete(DtsCategory category) {
       category.setDeleted(true);
       category.setUpdateTime(new Date());
        categoryMapper.updateById(category);
    }


//    @Override
//    public List<CatVo> findAll() {
//        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(DtsCategory::getDeleted,false);
//        List<DtsCategory> categoryList = categoryMapper.selectList(wrapper);
//        categoryList.stream().map(category -> {
//            CatVo catVo = new CatVo();
//            catVo.setValue(category.getPid());
//            catVo.setLabel(category.getName());
//            LambdaQueryWrapper<DtsCategory> wrapper1 = new LambdaQueryWrapper<>();
//            wrapper1.eq(DtsCategory::getLevel,)
//            catVo.setChildren();
//        }).collect(Collectors.toList())
//        return null;
//    }
}
